<script setup>
import { ref,reactive,onBeforeMount,unref } from 'vue'
import useAdminStore from '../../store/adminStore'
import useRolesStore from '../../store/rolesStore'
import { ElMessageBox } from 'element-plus'

const adminStore = useAdminStore()
const rolesStore = useRolesStore()

onBeforeMount(() => {
  // 获取角色数据
  rolesStore.getRolesAction()
})

const editId = ref('')
const usersForm = reactive({
  username: '',
  password: '',
  roleid: '',
  status: 1
})
const rules = reactive({
  username: [{ required: true, message: "请填写账号", trigger: "blur" }],
  password: [{ required: true, message: "请填写密码", trigger: "blur" }],
  roleid: [{ required: true, message: "请填写权限", trigger: "blur" }],
  status: [{ required: true, message: "请填写状态", trigger: "blur" }]
})
// 导出父组件需用到属性与方法
defineExpose({
  editId,usersForm
})

// 获取组件el-form上的ref
const usersFormRef = ref(null)

// 重置表单
const resetForm = () => {
  editId.value = ''
  usersForm.username = ''
  usersForm.password = ''
  usersForm.roleid = ''
  usersForm.status = 1
  // 关闭对话框
  adminStore.closeDialog()
}
// 提交表单
const submitForm = () => {
  // console.log(usersForm,'usersForm------')
  let form = unref(usersFormRef)
  form.validate((valid) => {
    if (valid) {
      // console.log('submit!')
      if(editId.value) {
        // 编辑
        // console.log(editId,'编辑-----')
        adminStore.editAdminAction({...usersForm,uid: editId.value})
      } else {
        // console.log(usersForm,'新增----')
        adminStore.addAdminAction(usersForm)
      }
      // 重置表单
      resetForm()
    } else {
      // console.log('error submit!')
      return false
    }
  })
}

const handleClose = (done) => {
  ElMessageBox.confirm('你确定要关闭吗？')
    .then(() => {
      done()
      // 重置表单
      resetForm()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<template>
  <!-- 对话框 -->
  <el-dialog
    :title="editId ? '编辑管理员' : '新增管理员'"
    v-model="adminStore.dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      label-width="100px"
      :model="usersForm"
      ref="usersFormRef"
      :rules="rules"
    >
      <el-form-item label="用户账号" prop="username">
        <el-input v-model="usersForm.username"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input v-model="usersForm.password"></el-input>
      </el-form-item>
      <el-form-item label="角色状态">
        <el-switch
          v-model="usersForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="用户权限" prop="roleid">
        <el-select v-model="usersForm.roleid" placeholder="请选择">
          <el-option
            v-for="item in rolesStore.roleList"
            :key="item.id"
            :label="item.rolename"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submitForm">{{ editId? '编辑':'新增' }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
